﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Com.CMS263.Util
{
    public static class FileUtil
    {
        /// <summary>
        /// 将内容写入指定文件
        /// </summary>
        /// <param name="filePath">文件的路径</param>
        /// <param name="content">文件内容</param>
        public static void WriteToFile(string filePath,string content)
        {
            if (!File.Exists(filePath))
            {
                File.Create(filePath);
            }
            StreamWriter sw = new StreamWriter(filePath, false);
            sw.Write(content);
            sw.Flush();
            sw.Close();
        }
        /// <summary>
        /// 见内容写入服务器上制定的文件
        /// </summary>
        /// <param name="filePath">服务器上的相对路径，不是绝对路径</param>
        /// <param name="content">文件内ring</param>
        public static void WriteToServerFile(string filePath, string content)
        {
            if (!File.Exists(StringUtil.ServerPath(filePath)))
            {
                FileStream fs= File.Create(StringUtil.ServerPath(filePath));
                fs.Close();
                fs.Dispose();
            }
            StreamWriter sw = new StreamWriter(StringUtil.ServerPath(filePath), false);
            sw.Write(content);
            sw.Flush();
            sw.Close();
        }
        /// <summary>
        /// 将内容追加入指定文件
        /// </summary>
        /// <param name="filePath">文件的路径</param>
        /// <param name="content">文件内容</param>
        public static void AppendToFile(string filePath, string content)
        {
            if (!File.Exists(filePath))
            {
                File.Create(filePath);
            }

            StreamWriter sw = new StreamWriter(filePath, true);
            sw.Write(content);
            sw.Flush();
            sw.Close();
        }

        public static string GetMenuText(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return "";
            }
            return File.ReadAllText(filePath, Encoding.UTF8);
        }
        /// <summary>
        ///通过相对路径名删除服务器上的文件
        /// </summary>
        /// <param name="serverPath">相对路径，会被自动转化成觉得路径</param>
        public static void DelServerFile(string serverPath)
        {
            if (File.Exists(StringUtil.ServerPath(serverPath))) 
            {
                File.Delete(StringUtil.ServerPath(serverPath));
            }
        }

    }
}
